package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author lanhaif
 * @since 2022-08-31
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

//    登陆方法
    @Override
    public String login(UcenterMember member) {
//        获取登陆手机号密码
        String mobile = member.getMobile();
        String password = member.getPassword();


//        手机号和密码非空判断
        if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"登陆失败");
        }

//        判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember==null){
            throw new GuliException(20001,"手机号不存在");
        }
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001,"密码错误");
        }
        if (mobileMember.getIsDisabled()) {
            throw new GuliException(20001,"用户被禁用");
        }

//        登陆成功，生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;

    }

    @Override
    public void register(RegisterVo registerVo) {
//        获取注册的数据
        String code=registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = MD5.encrypt(registerVo.getPassword());
        registerVo.setPassword(password);

        if(
                StringUtils.isEmpty(code) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(mobile)) {
            throw new GuliException(20001,"必填项为空");
        }

        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!redisCode.equals(code)) {
            throw new GuliException(20001,"验证码错误");
        }

//        判断手机号是否重复
        QueryWrapper<UcenterMember>wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        if (baseMapper.selectCount(wrapper)>0) {
            throw new GuliException(20001,"手机号已注册");
        }

//        数据库添加
        UcenterMember member=new UcenterMember();
        BeanUtils.copyProperties(registerVo,member);
        member.setIsDeleted(false);
        member.setAvatar("https://stock-study-by-lan.oss-cn-guangzhou.aliyuncs.com/520.jpg");
        baseMapper.insert(member);

    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
